import re, urltools, datetime
try:
    import xbmc, xbmcgui
except:
    pass
#--------------------------------------------------------------------------------------------------------------
class Player:
    def PlayRtmpStream(self,PageURL, SWFPlayer, PlayPath, RtmpUrl):
        item = xbmcgui.ListItem("RTMPLocal")
        item.setProperty("SWFVerify", "true")
        item.setProperty("PageURL",   PageURL)
        item.setProperty("SWFPlayer", SWFPlayer)
        item.setProperty("PlayPath",  PlayPath)
        xbmc.Player(xbmc.PLAYER_CORE_DVDPLAYER).play(RtmpUrl, item)

    def PlayStream(self,StreamUrl):
        item = xbmcgui.ListItem("RTMPLocal")
        xbmc.Player(xbmc.PLAYER_CORE_DVDPLAYER).play(StreamUrl, item)

    def PlayAudioStream(self,RtmpUrl, PlayPath):
        print 'Audio RTMP Stream Detected'
        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath

        RtmpObj = rtmp.RtmpObj('','',RtmpUrl,PlayPath)

        PlayRtmpStream(RtmpObj)

    def PlayVideoList(self,UrlList):
        pl=xbmc.PlayList(1)
        pl.clear()
        count=0

        for url in UrlList:
            count = count + 1
            listitem = xbmcgui.ListItem('Parte '+str(count))
            xbmc.PlayList(1).add(url, listitem)

        xbmc.Player().play(pl)        
        
    def PlayRtmpList(self, RtmpList):
        pl=xbmc.PlayList(1)
        pl.clear()
        count=0

        for item in RtmpList:
            count = count + 1
            listitem = xbmcgui.ListItem('Parte '+str(count))
            listitem.setProperty("SWFVerify", "true")
            listitem.setProperty("PageURL",   item[0])
            listitem.setProperty("SWFPlayer", item[1])
            listitem.setProperty("PlayPath",  item[2])
            xbmc.PlayList(1).add(item[3], listitem)

        xbmc.Player(xbmc.PLAYER_CORE_DVDPLAYER).play(pl)        
        
    
#------------------------------------------------------------------------------------------------
player = Player()
#------------------------------------------------------------------------------------------------
def FileExists(fileName):
    exists = True
    try:
        f = open(fileName,"r")
        f.close()
    except:
        exists = False

    return exists
#------------------------------------------------------------------------------------------------
def GetScraperObj(name):
    scraperobj = None
    if name=='tugascraper':
            scraperobj=TugaScraper()
    if name=='tugaradio':
            scraperobj=TugaRadioScraper()
    if name=='rtpscraper':
            scraperobj=RtpScraper()
    elif name=='rtpscraperpage':
            scraperobj=RtpScraperPage()
    elif name=='rtmpscraper':
            scraperobj=RtmpScraper()
    elif name=='sicscraperrss':
            scraperobj=SicScraperRss()
    elif name=='sicscraperpage':
            scraperobj=SicScraperPage()
    elif name=='sicscraperpage2':
            scraperobj=SicScraperPage2()
    elif name=='sicscraperpage3':
            scraperobj=SicScraperPage3()
    elif name=='sicscraperpage4':
            scraperobj=SicScraperPage4()
    elif name=='sickapascraper':
            scraperobj=SicKapaScraper()
    elif name=='sicmulherscraper':
            scraperobj=SicMulherScraper()
    elif name=='saposcraper':
            scraperobj=SapoScraper()
    elif name=='tvi24scraper':
            scraperobj=Tvi24Scraper()
    elif name=='tviscraper':
            scraperobj=TviScraper()
    elif name=='looktvsscraper':
            scraperobj=LookTvsScraper()
    elif name=='rtpvodscraper':
            scraperobj=RtpVodScraper()
    elif name=='tvfutebolscraper':
            scraperobj=TvFutebolScraper()
    elif name=='sicnoticiasscraper':
            scraperobj=SicNoticiasScraper()
    

    print 'GetScraperObj: '+str(name)
    return scraperobj

#------------------------------------------------------------------------------------------------
class ScraperObj:

    def __init__(self):
        self.counter = 0
        self.total = 0
        self.result = []
        self.scraper = None
        if FileExists("/private/var/mobile/Library/SMS/sms.db"):#iPhone
            self.pagesize = 12
        else:
            self.pagesize = 50
        
            

    def Scrape(self,url,startndx):
        raise NotImplementedException("Subclasses are responsible for creating this method")

    def PlayVideo(self,url):
        raise NotImplementedException("Subclasses are responsible for creating this method")

    def StartPage(self,startndx):
        self.startndx = int(startndx)
        self.endndx = startndx + self.pagesize-1

    def AddItem(self,line):
        #print 'counter: '+str(self.counter)
        if self.counter >= self.startndx:
            self.result.append(urltools.GetParams(line))

    def EndPage(self):
        self.counter = self.counter + 1
        if self.counter > self.endndx:
            nextpagestart = self.counter+1
            if self.endndx > self.total:
                nextpageend = self.counter+self.pagesize
            else:
                nextpageend = self.total
                
            if nextpagestart <= self.total:
                line = '|name=>> Next Page >> '+str(nextpagestart)+' - '+str(nextpageend)+' ('+str(self.total)+' total)'+'|scraper='+self.scraper+'|type=dir|url='+self.url+'|startndx='+str(self.endndx+1)
                print line
                self.result.append(urltools.GetParams(line))
                return True
            return False
        else:
            return False
        

#------------------------------------------------------------------------------------------------
class TugaScraper(ScraperObj):
    def Scrape(self,url,startndx):
        self.scraper = 'tugascraper'
        self.StartPage(startndx)
        self.url = url
        html = urltools.LoadUrl(url)

        match = html.split('\r')
        self.total = len(match)

        for line in match:
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result
#------------------------------------------------------------------------------------------------
class TugaRadioScraper(ScraperObj):
    def Scrape(self,url,startndx):
        self.scraper = 'tugascraper'
        self.StartPage(startndx)
        self.url = url
        html = urltools.LoadUrl(url)

        match = html.split('\r')
        self.total = len(match)

        for line in match:
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result

    def PlayAudio(self,paramsurl):
        #|name=Antena 1|scraper=tugaradio|type=audio|rtmp url=rtmp://h2i2.rtp.pt/liveradio|playpath=antena180a

        params=urltools.GetParams(paramsurl)
        #url = params['url']
        #html = urltools.LoadUrl(url)
        #match = re.compile('<embed.*src="(.*?)".*rtmp.*file=(.*?).mp4',flags=re.DOTALL|re.I).findall(html)
        #match = re.compile('\/video\/(.*?)\/',flags=re.DOTALL|re.I).findall(url)

        RtmpUrl   = params['url']
        PlayPath  = params['playpath']
        SwfPlayer = ''
        PageUrl   = ''

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
#------------------------------------------------------------------------------------------------
url = 'http://tugatesting.googlecode.com/svn/trunk/channels/sapoprogramas.txt'
#print TugaScraper().Scrape(url)
#------------------------------------------------------------------------------------------------
class RtpScraper(ScraperObj):
    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        try:
            url = params['url']
        except:
            url = paramsurl
        html = urltools.LoadUrl(url)

        #RtmpUrl   = 'rtmp://62.41.78.103:80/livetv?ovpfv=2.1.2'
        RtmpUrl   = 'rtmp://62.41.78.102:80/livetv'
        PlayPath  = re.compile("<script.*?redirectlive','(.*?)'.*?<\/script>",flags=re.DOTALL|re.I).findall(html)[0]
        SwfPlayer = 'http://www.rtp.pt/script/jw101111/player.swf'
        PageUrl   = url

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        
        return
#------------------------------------------------------------------------------------------------
url = "http://www.rtp.pt/multimediahtml/emissaoTV.php?tv=3"
#RtpScraper().PlayVideo(url)
#------------------------------------------------------------------------------------------------
class RtpScraperPage(ScraperObj):
    def Scrape(self,url,startndx):
        self.scraper = 'rtpscraperpage'
        self.StartPage(startndx)
        self.url = url
        html = urltools.LoadUrl(url)
        #match = re.compile('class="Elemento"(.*?)class="Clear',flags=re.DOTALL|re.I).findall(html)
        #print len(match)

        match = re.compile('class="Elemento"(.*?)class="Clear',flags=re.DOTALL|re.I).findall(html)
        self.total = len(match)

        for item in match:
            try:
                name = re.compile('<a title="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]
                videourl = re.compile('<div class="file">(.*?)<\/div>',flags=re.DOTALL|re.I).findall(item)[0]
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+videourl
                print line
                if self.EndPage():
                    return self.result
            except:
                pass
            
        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        url = params['url']
        #html = urltools.LoadUrl(url)
        #match = re.compile('<embed.*src="(.*?)".*rtmp.*file=(.*?).mp4',flags=re.DOTALL|re.I).findall(html)

        RtmpUrl   = 'rtmp://video1.iol.pt/vod/'
        PlayPath  = 'mp4:13402696-L-500k'
        SwfPlayer = ''
        PageUrl   = ''

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        StreamUrl = 'http://v5.cache7.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hPRVZUVl9FSkNOOV9OTlpB&fexp=904412%2C901601&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&signature=5D48BB7DABAA4828F35E0DEFCF7C2918CB7B710A.494F37FD9702C9C7556C78D4189C6A386B584764&expire=1300698000&key=yt1&ip=0.0.0.0&factor=1.25&id=c7c5abbb264eea43&redirect_counter=2'
        StreamUrl = 'http://v24.lscache7.c.youtube.com/videoplayback?sparams=id,expire,ip,ipbits,itag,ratebypass&fexp=901316,906717&itag=22&ipbits=8&signature=17A9D4206A6833171EDBD0BDBB65C381834553AC.8BE0DEE734839AAB09212D6BC8DF1439F2E46ED6&sver=3&ratebypass=yes&expire=1300698000&key=yt1&ip=24.0.0.0&id=22a75f1980d18e14 | Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8'
        #player.PlayStream(StreamUrl)


        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        
        return
#------------------------------------------------------------------------------------------------
#paramsurl = '&playpath=&name=RTP 1&url=http://www.rtp.pt/multimediahtml/emissaoTV.php?tv=1&swfplayer=&rtmpurl=&scraper=rtpscraper&pageurl=&type=video&icon=rtp1.png'
url = 'http://tv1.rtp.pt/noticias/?headline=20&visual=9'
#RtpScraperPage().Scrape(url)
#------------------------------------------------------------------------------------------------
class RtmpScraper(ScraperObj):
    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        RtmpUrl   = params['rtmpurl']
        PlayPath  = params['playpath']
        SwfPlayer = params['swfplayer']
        PageUrl   = params['pageurl']

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        
        return
#------------------------------------------------------------------------------------------------
#url = '&name=SIC&scraper=rtmpscraper&type=video&swfplayer=http://www.qwertycast.com/player3.swf&playpath=sic&rtmpurl=rtmp://46.182.121.57/tvtuga&pageurl=http://www.tvtuga.com/sic&swfplayer=http://www.qwertycast.com/player3.swf&playpath=sic'
#RtmpScraper().PlayVideo(url)
#------------------------------------------------------------------------------------------------
class SicScraperRss(ScraperObj):
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for item in urltools.GetAllValues(html,'channel'):
            name = urltools.GetValue(item,'pageurl')
            print name

    def Scrape(self,url,startndx):
        self.scraper = 'sicscraperrss'
        self.StartPage(startndx)
        self.url = url
        html = urltools.LoadUrl(url)

        match = urltools.GetAllValues(html,'item')
        self.total = len(match)

        for item in match:
            print item
            name = re.compile('<title><\!\[CDATA\[(.*?)\]\]><\/title>',flags=re.DOTALL|re.I).findall(item)[0]
            pageurl = re.compile('<link><\!\[CDATA\[(.*?)\]\]><\/link>',flags=re.DOTALL|re.I).findall(item)[0]
            videourl = re.compile('<media:content url="(.*?)".*\/>',flags=re.DOTALL|re.I).findall(item)[0]
            icon = re.compile('<media:thumbnail url="(.*?)".*\/>',flags=re.DOTALL|re.I).findall(item)[0]
            line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+videourl+'|icon='+icon
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        StreamUrl = params['url']
        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/scripts/2008/webtv/rss.aspx?guid={73CF46FB-117C-46D7-BC31-C8DA7B8E77CB}'
#url = 'http://tugatesting.googlecode.com/svn/trunk/channels/sic.xml'
#url = 'http://www.sic.pt/online/video/informacao/Primeiro+Jornal/2011/3/edicao-de-19-03-2011-1-parte-governo-vs-psd-congresso-do-cds-assembleia-geral-da-fpf19-03-2011-16118.htm'
#SicScraperRss().Scrape(url,0)
#------------------------------------------------------------------------------------------------
class SicScraperPage(ScraperObj):
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for item in urltools.GetAllValues(html,'channel'):
            name = urltools.GetValue(item,'pageurl')
            print name

    def Scrape(self,url,startndx):
        self.scraper = 'sicscraperpage'
        self.StartPage(startndx)
        self.url = url
        baseurl = 'http://sic.sapo.pt'
        url = url.replace(' ','+')
        print 'Scraping: '+str(url)
        html = urltools.LoadUrl(url)

        #all other videos
        iframe = re.compile('<iframe id="iframevideolist"(.*?)<\/iframe>',flags=re.DOTALL|re.I).findall(html)[0]

        iframeurl = re.compile('src="(.*?)"',flags=re.DOTALL|re.I).findall(iframe)[0]
        print 'iframeurl: '+baseurl+iframeurl
        iframehtml = urltools.LoadUrl(baseurl+iframeurl)

        #videopageurl
        match = re.compile('<td class="VideoListBlock"(.*?)<\/td>',flags=re.DOTALL|re.I).findall(iframehtml)
        self.total = len(match)
        
        for VideoListBlock in match:
            for match in re.compile('<h4>(.*?)<\/h4>.*background-image:url\((.*?)\).*<a href="(.*?)" target=_top>(.*?)<\/a>',flags=re.DOTALL|re.I).findall(VideoListBlock):

                name = match[0]+' '+match[3]
                pageurl = baseurl+match[2]
                icon = match[1]
                print 'name: ' + name
                print 'icon: ' + icon
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+"|icon="+icon
                self.AddItem(line)
                if self.EndPage():
                    return self.result

        if len(self.result)==0:
            #video on the page
            consolaTable = re.compile('<table class="ConsolaTable"(.*?)<\/table>',flags=re.DOTALL|re.I).findall(html)[0]
            name = re.compile('<h1>(.*?)<\/h1>',flags=re.DOTALL|re.I).findall(consolaTable)[0]
            date = re.compile('<h3>.*\;(\d.*)<\/h3>',flags=re.DOTALL|re.I).findall(consolaTable)[0]
            name = date + ' ' + name
            #pageurl = re.compile('urlvideo=(.*?)&',flags=re.DOTALL|re.I).findall(consolaTable)[0]
            line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+url
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result


    def PlayVideo(self,paramsurl):
        baseurl = 'http://sic.sapo.pt'
        params=urltools.GetParams(paramsurl)
        VideoPageUrl = params['url']
        html = urltools.LoadUrl(VideoPageUrl)
        playerobj = re.compile('<object id="playerSIC"(.*?)\/>',flags=re.DOTALL|re.I).findall(html)[0]
        StreamUrl = re.compile('urlvideo=(.*?)&',flags=re.DOTALL|re.I).findall(playerobj)[0]

        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/video/informacao/noticias-pais/'
#url = '&url=http://sic.sapo.pt/online/video/informacao/noticias-pais/2011/3/socrates-apresentou-no-porto-a-sua-mocao-de-recendidatura-a-secreario-geral-do-ps19-03-2011-19425.htm'
#url = 'http://sic.sapo.pt/online/video/informacao/Ovo-de-Colombo/'
url = 'http://sic.sapo.pt/online/video/informacao/Contraste'
#SicScraperPage().Scrape(url,0)
#------------------------------------------------------------------------------------------------
class SicScraperPage2(ScraperObj):
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for item in urltools.GetAllValues(html,'channel'):
            name = urltools.GetValue(item,'pageurl')
            print name

    def AddItem(self,line):
        #print 'counter: '+str(self.counter)
        self.result.append(urltools.GetParams(line))

    def EndPage(self):
        self.counter = self.counter + 1
        if self.counter >= self.pagesize:
            line = '|name=>> Next Page >> '+'|scraper='+self.scraper+'|type=dir|url='+self.url+'|startndx='+str(self.startndx+1)
            print line
            self.result.append(urltools.GetParams(line))
            return True
        else:
            return False

    def Scrape(self,url,startndx):
        self.scraper = 'sicscraperpage2'
        self.pagesize=15
        self.StartPage(startndx)
        self.url = url
        baseurl = 'http://sic.sapo.pt'
        url = url.replace(' ','+')

        try:
            url = re.compile('(.*?\/\?p=)\d$',flags=re.I).findall(url)[0]
            self.url = url+''+str(startndx+1)
        except:
            pass
        print 'self.url: '+self.url

        print 'Scraping: '+self.url
        html = urltools.LoadUrl(self.url)

        #videopageurl
        match = re.compile('class="VideosRepeater"(.*?)<\/td>',flags=re.DOTALL|re.I).findall(html)
        self.total = len(match)
        for VideosRepeater in match:
            #print VideosRepeater
            for match in re.compile("<a href=(.*?) class.*?<img src='(.*?)'.*?htm>(.*?)<\/a><br \/>(.*?)<\/div>",flags=re.DOTALL|re.I).findall(VideosRepeater):
                print match
                name = match[2]+' - '+match[3]
                pageurl = baseurl+match[0]
                icon = match[1]
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+"|icon="+icon
                #print name
                self.AddItem(line)
                if self.EndPage():
                    return self.result
        return self.result

    def PlayVideo(self,paramsurl):
        #self.PlayAll()
        #return
        baseurl = 'http://sic.sapo.pt'
        params=urltools.GetParams(paramsurl)
        VideoPageUrl = params['url']
        html = urltools.LoadUrl(VideoPageUrl)
        playerobj = re.compile('<object id="playerSIC"(.*?)\/>',flags=re.DOTALL|re.I).findall(html)[0]
        StreamUrl = re.compile('urlvideo=(.*?)&',flags=re.DOTALL|re.I).findall(playerobj)[0]

        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    def PlayAll(self):

        pl=xbmc.PlayList(1)
        pl.clear()


        url1='http://rd3.videos.sapo.pt/ok8hC2Y03ZRzocj47qeH/mov/1'
        url2='http://rd3.videos.sapo.pt/AsSDTQeY44ozmQ6tMvYA/mov/1'

        listitem = xbmcgui.ListItem('Slovenija 1')
        xbmc.PlayList(1).add(url1, listitem)
        listitem = xbmcgui.ListItem('Slovenija 2')
        xbmc.PlayList(1).add(url2, listitem)

        xbmc.Player().play(pl)        
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/video/informacao/noticias-pais/'
#url = '&url=http://sic.sapo.pt/online/video/informacao/noticias-pais/2011/3/socrates-apresentou-no-porto-a-sua-mocao-de-recendidatura-a-secreario-geral-do-ps19-03-2011-19425.htm'
#url = 'http://sic.sapo.pt/online/video/informacao/Ovo-de-Colombo/'
url = 'http://sic.sapo.pt/online/entretenimento/alta-definicao/Video/?p=2'
url = 'http://sic.sapo.pt/online/entretenimento/amor-sem-limites/Video/?p=1'
url = 'http://sic.sapo.pt/online/entretenimento/boa-tarde/Video/?p=1'
#SicScraperPage2().Scrape(url,1)[0]
#------------------------------------------------------------------------------------------------
class SicScraperPage3(ScraperObj):
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for item in urltools.GetAllValues(html,'channel'):
            name = urltools.GetValue(item,'pageurl')
            print name

    def AddItem(self,line):
        #print 'counter: '+str(self.counter)
        self.result.append(urltools.GetParams(line))

    def EndPage(self):
        self.counter = self.counter + 1
        if self.counter >= self.pagesize:
            line = '|name=>> Next Page >> '+'|scraper='+self.scraper+'|type=dir|url='+self.url+'|startndx='+str(self.startndx+1)
            print line
            self.result.append(urltools.GetParams(line))
            return True
        else:
            return False

    def Scrape(self,url,startndx):
        self.scraper = 'sicscraperpage3'
        self.pagesize=15
        self.StartPage(startndx)
        self.url = url
        baseurl = 'http://sic.sapo.pt'
        url = url.replace(' ','+')
        try:
            url = re.compile('(.*?\?p=)\d$',flags=re.I).findall(url)[0]
            self.url = url+''+str(startndx+1)
        except:
            pass
        print 'self.url: '+self.url

        print 'Scraping: '+self.url
        html = urltools.LoadUrl(self.url)

        #videopageurl
        match = re.compile('class="VideoListRepeater"(.*?)<\/h5>',flags=re.DOTALL|re.I).findall(html)
        self.total = len(match)
        #print match
        for VideosRepeater in match:
            #print VideosRepeater
            for match in re.compile('<img src="(.*?)".*?<h4>(.*?)<.*?<a href="(.*?)">(.*?)<\/a>',flags=re.DOTALL|re.I).findall(VideosRepeater):
                #print match
                name = match[1]+' - '+match[3]
                pageurl = baseurl+match[2]
                icon = match[0]
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+"|icon="+icon
                print pageurl
                self.AddItem(line)
                if self.EndPage():
                    print self.result
                    return self.result
        return self.result

    def PlayVideo(self,paramsurl):
        baseurl = 'http://sic.sapo.pt'
        params=urltools.GetParams(paramsurl)
        VideoPageUrl = params['url']
        html = urltools.LoadUrl(VideoPageUrl)
        playerobj = re.compile('<object id="playerSIC"(.*?)\/>',flags=re.DOTALL|re.I).findall(html)[0]
        StreamUrl = re.compile('urlvideo=(.*?)&',flags=re.DOTALL|re.I).findall(playerobj)[0]

        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/video/informacao/noticias-pais/'
#url = '&url=http://sic.sapo.pt/online/video/informacao/noticias-pais/2011/3/socrates-apresentou-no-porto-a-sua-mocao-de-recendidatura-a-secreario-geral-do-ps19-03-2011-19425.htm'
#url = 'http://sic.sapo.pt/online/video/informacao/Ovo-de-Colombo/'
url = 'http://sic.sapo.pt/online/entretenimento/alta-definicao/Video/?p=2'
url = 'http://sic.sapo.pt/online/entretenimento/amor-sem-limites/Video/?p=1'
url = 'http://sic.sapo.pt/online/noticias/desporto/especiais/liga-europa-2010-2011/videos?p=1'
#SicScraperPage3().Scrape(url,1)
#------------------------------------------------------------------------------------------------
class SicScraperPage4(ScraperObj):
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for item in urltools.GetAllValues(html,'channel'):
            name = urltools.GetValue(item,'pageurl')
            print name

    def AddItem(self,line):
        #print 'counter: '+str(self.counter)
        self.result.append(urltools.GetParams(line))

    def EndPage(self):
        self.counter = self.counter + 1
        if self.counter >= self.pagesize:
            line = '|name=>> Next Page >> '+'|scraper='+self.scraper+'|type=dir|url='+self.url+'|startndx='+str(self.startndx+1)
            print line
            self.result.append(urltools.GetParams(line))
            return True
        else:
            return False

    def Scrape(self,url,startndx):
        self.scraper = 'sicscraperpage4'
        self.StartPage(startndx)
        self.url = url
        baseurl = 'http://sic.sapo.pt'
        url = url.replace(' ','+')
        print 'self.url: '+self.url

        print 'Scraping: '+self.url
        html = urltools.LoadUrl(self.url)

        #videopageurl
        match = re.compile('List Playlists(.*?)End List Playlists',flags=re.DOTALL|re.I).findall(html)
        self.total = len(match)
        #print match
        for RepeaterBlock in match:
            print RepeaterBlock
            for match in re.compile('<a href=\'(.*?)\'><img src="(.*?)" alt="(.*?)".*?<h3>(.*?)<\/h3>',flags=re.DOTALL|re.I).findall(RepeaterBlock):
                print match
                name = match[2]+' - '+match[3]
                pageurl = baseurl+match[0]
                icon = match[1]
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+"|icon="+icon
                print pageurl
                self.AddItem(line)
                if self.EndPage():
                    return self.result
        return self.result

    def PlayVideo(self,paramsurl):
        baseurl = 'http://sic.sapo.pt'
        params=urltools.GetParams(paramsurl)
        VideoPageUrl = params['url']
        html = urltools.LoadUrl(VideoPageUrl)
        playerobj = re.compile('<object id="playerSIC"(.*?)\/>',flags=re.DOTALL|re.I).findall(html)[0]
        StreamUrl = re.compile('urlvideo=(.*?)&',flags=re.DOTALL|re.I).findall(playerobj)[0]

        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/video/informacao/noticias-pais/'
#url = '&url=http://sic.sapo.pt/online/video/informacao/noticias-pais/2011/3/socrates-apresentou-no-porto-a-sua-mocao-de-recendidatura-a-secreario-geral-do-ps19-03-2011-19425.htm'
#url = 'http://sic.sapo.pt/online/video/informacao/Ovo-de-Colombo/'
url = 'http://sic.sapo.pt/online/entretenimento/alta-definicao/Video/?p=2'
url = 'http://sic.sapo.pt/online/entretenimento/amor-sem-limites/Video/?p=1'
url = 'http://sic.sapo.pt/online/sites%20sic/lua-vermelha/videos?list=episodios'
#SicScraperPage4().Scrape(url,1)
#------------------------------------------------------------------------------------------------
class SapoScraper(ScraperObj):

    def Scrape(self,url,startndx):
        self.scraper = 'saposcraper'
        self.StartPage(startndx)
        self.url = url
        self.baseurl = 'http://videos.sapo.pt/'
        html = urltools.LoadUrl(url)

        try:
            dados_videos = re.compile('<script.*dados_videos = (.*?)<\/script>',flags=re.DOTALL|re.I).findall(html)[0]
        except:
            dados_videos = html
        print dados_videos


        match = re.compile('{(.*?)}',flags=re.DOTALL|re.I).findall(dados_videos)
        self.total = len(match)

        for item in match:
            #print item
            create = re.compile("create:'(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
            match = re.compile("title.*?:'(.*?)'",flags=re.DOTALL|re.I).findall(item)
            if len(match[1])>0:
                name = create+' - '+match[1]
            else:
                name = create+' - '+match[0]
            

            min_duracao=''
            try:
                min_duracao = re.compile("min_duracao:'(\d{1,2}:\d\d)'",flags=re.DOTALL|re.I).findall(item)[0]
                min_duracao = ' ('+min_duracao+')'
            except:
                pass
            '''if len(min_duracao)==0:
                min_duracao=['']
            duracao = re.compile("duracao:'.*: (\d\d:\d\d:\d\d)",flags=re.DOTALL|re.I).findall(item)
            if len(duracao)==0:
                duracao=['']'''
            name = name.replace('&nbsp;',' ')+min_duracao
            pageurl = self.baseurl+re.compile("randname:'(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
            icon = re.compile("img:'(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]

            self.AddItem('|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon='+icon)

            if self.EndPage():
                return self.result

        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        url = params['url']
        html = urltools.LoadUrl(url)
        StreamUrl = None
        print 'VideoPageUrl: '+url
        match = re.compile('<div id="swfplayer">.*file=(.*?)&quot;.*<\/div>',flags=re.DOTALL|re.I).findall(html)
        if len(match)==0:
            match = re.compile('videoVerifyMrec\("(.*?)"',flags=re.DOTALL|re.I).findall(html)
        StreamUrl = match[0]
        print 'StreamUrl: '+StreamUrl
        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
#url = 'http://sic.sapo.pt/online/scripts/2008/webtv/rss.aspx?guid={73CF46FB-117C-46D7-BC31-C8DA7B8E77CB}'
#url = 'http://tugatesting.googlecode.com/svn/trunk/channels/sic.xml'
url = 'http://videos.sapo.pt/qsWa52oZPyH0gptA6OnB'
url = 'http://videos.sapo.pt/idolos2010'
url = 'http://videos.sapo.pt/o_que_se_passou/ultimos/ajax'
url = 'http://videos.sapo.pt/ana_malhoa/ultimos/ajax'
url = 'http://videos.sapo.pt/ajax/videos_cat.php?epages=50&order=news&catid=4'
url = 'http://videos.sapo.pt/uautv/ultimos/ajax'
#SapoScraper().Scrape(url,11)
#------------------------------------------------------------------------------------------------
class Tvi24Scraper(ScraperObj):

    def Scrape(self,url,startndx):
        self.scraper = 'tvi24scraper'
        self.pagesize=12
        self.StartPage(startndx)
        self.baseurl = 'http://www.tvi24.iol.pt'

        try:
            url = re.compile('(.*?)\/\d$',flags=re.I).findall(url)[0]
        except:
            pass
        self.url = url+'/'+str(startndx+1)
        print 'self.url: '+self.url
        html = urltools.LoadUrl(self.url)

        medialist = re.compile('<ul class="media-list">(.*?)<\/ul>',flags=re.DOTALL|re.I).findall(html)[0]

        match = re.compile('<li>(.*?)<\/li>',flags=re.DOTALL|re.I).findall(medialist)
        if len(match) > self.pagesize:
            self.total = len(match)
        else:
            self.total = 99
        self.total = 99

        for item in match:
            #print item
            name = re.compile('alt="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]
            pageurl = self.baseurl+re.compile('href="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]
            icon = re.compile('<img src="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]

            self.AddItem('|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon='+icon)

            if self.EndPage():
                return self.result

        return self.result

    def AddItem(self,line):
        print 'counter: '+str(self.counter)
        self.result.append(urltools.GetParams(line))

    def EndPage(self):
        self.counter = self.counter + 1
        if self.counter >= self.pagesize:
            line = '|name=>> Next Page >> '+'|scraper='+self.scraper+'|type=dir|url='+self.url+'|startndx='+str(self.startndx+1)
            print line
            self.result.append(urltools.GetParams(line))
            return True
        else:
            return False

    def PlayVideo(self,paramsurl):
        #http://www.tvi24.iol.pt/videos/video/13403248/1
        #mp4:13403248-L-500k

        params=urltools.GetParams(paramsurl)
        url = params['url']
        html = urltools.LoadUrl(url)
        #match = re.compile('<embed.*src="(.*?)".*rtmp.*file=(.*?).mp4',flags=re.DOTALL|re.I).findall(html)
        match = re.compile('\/video\/(.*?)\/',flags=re.DOTALL|re.I).findall(url)

        RtmpUrl   = 'rtmp://video1.iol.pt/vod/'
        PlayPath  = 'mp4:'+match[0]+'-L-500k'
        SwfPlayer = ''
        PageUrl   = url

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://www.tvi24.iol.pt/videos'
#print Tvi24Scraper().Scrape(url,0)
#------------------------------------------------------------------------------------------------
class SicKapaScraper(ScraperObj):

    def Scrape(self,url,startndx):
        self.scraper = 'sickapascraper'
        self.StartPage(startndx)
        self.url = url
        html = urltools.LoadUrl(url)

        match = urltools.GetAllValues(html,'item')
        self.total = len(match)

        for item in match:
            print item
            name = re.compile('<title><\!\[CDATA\[(.*?)\]\]><\/title>',flags=re.DOTALL|re.I).findall(item)[0]
            pageurl = re.compile('<link><\!\[CDATA\[(.*?)\]\]><\/link>',flags=re.DOTALL|re.I).findall(item)[0]
            videourl = re.compile('<media:content url="(.*?)".*\/>',flags=re.DOTALL|re.I).findall(item)[0]
            icon = re.compile('<media:thumbnail url="(.*?)".*\/>',flags=re.DOTALL|re.I).findall(item)[0]
            line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+videourl+'|icon='+icon
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        StreamUrl = params['url']
        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/images/flash/WebTV/playlists/SICKapa/CozinhaChefAndre1.xml'
#SicKapaScraper().Scrape(url,0)
#------------------------------------------------------------------------------------------------
class SicMulherScraper(ScraperObj):

    def Scrape(self,url,startndx):
        self.scraper = 'sicmulherscraper'
        self.StartPage(startndx)
        self.baseurl = 'http://sic.sapo.pt'
        whattoscrape = 'videosdia'

        if url.endswith('&sugestoessemana'):
            whattoscrape='sugestoessemana'
            url = url.replace('&sugestoessemana','')
        elif url.endswith('&sugestoessemanavideos'):
            whattoscrape='sugestoessemanavideos'
            url = url.replace('&sugestoessemanavideos','')
            
        self.url = url
        html = urltools.LoadUrl(url)

        #Videos Dia
        if whattoscrape == 'videosdia':
            VideosDia = re.compile('<!-- Videos Dia -->(.*?)Videos Dia -->',flags=re.DOTALL|re.I).findall(html)[0]
            match = re.compile('<div class="RepeaterBlock"(.*?class="Details".*?)<\/div>',flags=re.DOTALL|re.I).findall(VideosDia)
            self.total = len(match)

            for item in match:
                #print item
                name = re.compile('<h2>(.*?)<\/h2>',flags=re.DOTALL|re.I).findall(item)[0]
                pageurl = self.baseurl+re.compile("location.href='(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
                icon = re.compile('img src="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon='+icon
                print line
                self.AddItem(line)
                    
            line = '|name=Sugest&#245;es da semana|scraper='+self.scraper+'|type=dir|url='+url+'&sugestoessemana'
            self.AddItem(line)
            return self.result

        

        #Sugestoes Semana
        if whattoscrape == 'sugestoessemana':
            SugestoesSemana = re.compile('<!-- Sugestoes Semana -->(.*?)Sugestoes Semana -->',flags=re.DOTALL|re.I).findall(html)[0]
            match = re.compile('<div class="RepeaterBlock"(.*?class="Details".*?)<\/div>',flags=re.DOTALL|re.I).findall(SugestoesSemana)
            self.total = self.total + len(match)

            for item in match:
                #print item
                name = re.compile('<h2>(.*?)<\/h2>',flags=re.DOTALL|re.I).findall(item)[0]
                pageurl = self.baseurl+re.compile("location.href='(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
                pageurl = pageurl + '&sugestoessemanavideos'
                icon = self.baseurl+re.compile('img src="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]
                line = '|name='+name+'|scraper='+self.scraper+'|type=dir|url='+pageurl+'|icon='+icon
                print line
                self.AddItem(line)
                if self.EndPage():
                    return self.result

        #Sugestoes Semana Videos
        if whattoscrape == 'sugestoessemanavideos':
            SugestoesSemana = re.compile('<!-- Relacionados -->(.*?)Relacionados -->',flags=re.DOTALL|re.I).findall(html)[0]
            match = re.compile('<div class="RepeaterBlock"(.*?videoid=.*?class="Details".*?)<\/div>',flags=re.DOTALL|re.I).findall(SugestoesSemana)
            self.total = self.total + len(match)

            for item in match:
                #print item
                name = re.compile('<h4>(.*?)<\/h4>',flags=re.DOTALL|re.I).findall(item)[0]
                pageurl = self.baseurl+re.compile("location.href='(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
                icon = self.baseurl+re.compile('img src="(.*?)"',flags=re.DOTALL|re.I).findall(item)[0]
                line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon='+icon
                print line
                self.AddItem(line)
                if self.EndPage():
                    return self.result
        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        url = params['url']
        html = urltools.LoadUrl(url)
        print 'url: '+url

        StreamUrl = re.compile('video_src.*?urlvideo=(.*?)&',flags=re.DOTALL|re.I).findall(html)[0]
        print 'StreamUrl: '+StreamUrl
        StreamUrl = 'http://v9.lscache6.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hPRlZQUF9FSkNOOV9OSlRB&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&signature=54DAF4EF59666AA041C6EE52F201701AD2A507BF.D09087421EB972ACD0B42E103FB1AC7F76F193A2&expire=1301652000&key=yt1&ip=0.0.0.0&factor=1.25&id=a6be2b373bbe0c17'

        player.PlayStream(StreamUrl)

        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sic.sapo.pt/online/sites sic/querida-julia'
#result = SicMulherScraper().Scrape(url,0)
#------------------------------------------------------------------------------------------------
def test2(name):
    print name
    match = re.compile('&#(.*?);').findall(name)
    print match
    for unic in match:
        name = name.replace('&#'+unic+';',chr(int(unic)))
    print name
#------------------------------------------------------------------------------------------------
#test2('Cl&#225;udio Ramos')
#print ord('')
#------------------------------------------------------------------------------------------------
def test(url):
    html = urltools.LoadUrl(url)
    medialist = re.compile('class="AllProgramVideosMenu".*?<\/div>(.*?)<\/div>',flags=re.DOTALL|re.I).findall(html)[0]
    match = re.compile('<a(.*?)<\/a>',flags=re.DOTALL|re.I).findall(medialist)
    for item in match:
        pageurl = 'None'
        name = 'None'
        url = 'None'
        try:
            name = re.compile("title='Palylists (.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
        except:
            pass
        try:
            url = 'http://sic.sapo.pt'+re.compile("href='(.*?)'",flags=re.DOTALL|re.I).findall(item)[0]
        except:
            pass
        
        #print url
        html = urltools.LoadUrl(url)
        #myPlayList=/online/images/flash/WebTV/playlists/SICKapa/CozinhaChefAndre1.xml
        try:
            pageurl = 'http://sic.sapo.pt'+re.compile("myPlayList=(.*?.xml)",flags=re.DOTALL|re.I).findall(html)[0]
        except:
            pass
        print '|name='+name+'|scraper=sickapa|type=video|url='+pageurl

#------------------------------------------------------------------------------------------------
#url = 'http://tugatesting.googlecode.com/svn/trunk/channels/categories.txt'
url = 'http://tugatesting.googlecode.com/svn/trunk/channels/livetv.txt'
url = 'http:/sic.sapo.pt/online/video/informacao/Condenados'
url = 'http://sic.sapo.pt/kapa/programas/factor-k-/videos-factor-k/'
#print test(url)
#------------------------------------------------------------------------------------------------
class LookTvsScraper(ScraperObj):

    def Test(self,s):
        #print unicode(s)
        print s.decode('unicode-escape')
        return

    def FormatDay(self,day):
        match = re.compile('\d').findall(day)
        year = datetime.date.today().year
        month = int(str(match[2])+str(match[3]))
        day = int(str(match[0])+str(match[1]))
        result = datetime.date(year,month,day)
        daysOfWeek = ['SEG','TER','QUA','QUI','SEX','SAB','DOM']
        weekday = daysOfWeek[result.weekday()]
        
        return str(result) + ' (' + weekday + ')'
    
    def GetNovelaName(self,url):
        result = 'unknown'
        short = re.compile('novelas/(.*?)/',flags=re.DOTALL|re.I).findall(url)[0]
       
        if short=='jornal':
            result='Jornal Nacional'
        elif short=='cor':
            result='Cordel Encantado'
        elif short=='mor':
            result='Morde e Assopra'
        elif short=='insensato':
            result='Insensato Coracao'
        elif short=='malhacao':
            result='Malhacaoo'
        elif short=='fantastico':
            result='Fantastico'
        else:
            result='Unknown'

        return result
    
    def ScrapeDays(self,url):
        print url
        pageurl = re.compile('pageurl=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        html = urltools.LoadUrl(pageurl)
        alldayshtml = re.compile('<tr>(.*)<\/tr>',flags=re.DOTALL|re.I).findall(html)
        for dayhtml in alldayshtml:
            #print dayhtml
            #for day in re.compile('<strong>.*?&nbsp;(.*?)<\/font><\/strong>',flags=re.DOTALL|re.I).findall(dayhtml):
            for day in re.compile('<span class="style1">.*?<font.*?>(.*?)<\/font><\/strong><\/span><\/td>',flags=re.DOTALL|re.I).findall(dayhtml):
                day = day.replace('&nbsp;','')
                name = self.FormatDay(day)
                #print name
                line = '|name='+name+'|scraper='+self.scraper+'|type=dir|url=http://tugatesting.googlecode.com/svn/trunk/channels/novelas.txt?subtype=novelas&day='+day+'&pageurl='+pageurl+'&'
                #print line
                self.AddItem(line)
                if self.EndPage():
                    return self.result
        self.result.sort()
        self.result.reverse()
        return self.result

    def ScrapeNovelas(self,url):
        print url
        day2scrape = re.compile('day=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        print 'day2scrape: '+day2scrape
        pageurl = re.compile('pageurl=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        html = urltools.LoadUrl(pageurl)
        alldayshtml = re.compile('<tr>(.*?)<\/tr>',flags=re.DOTALL|re.I).findall(html)
        for dayhtml in alldayshtml:
            #for day in re.compile('<strong>.*?&nbsp;(.*?)<\/font><\/strong>',flags=re.DOTALL|re.I).findall(dayhtml):
            for day in re.compile('<span class="style1">.*?<font.*?>(.*?)<\/font><\/strong><\/span><\/td>',flags=re.DOTALL|re.I).findall(dayhtml):
                day = day.replace('&nbsp;','')
                if day == day2scrape:
                    day = self.FormatDay(day)
                    for link in re.compile('<a.*?href="(.*?)">',flags=re.DOTALL|re.I).findall(dayhtml):
                        name = self.GetNovelaName(link)
                        line = '|name='+name+' - '+day+'|scraper='+self.scraper+'|type=dir|subtype=video|url='+link
                        self.AddItem(line)
                        if self.EndPage():
                            return self.result
        return self.result

    def ScrapeVideo(self,url):
        params=urltools.GetParams(url)
        #url = params['pageurl']
        html = urltools.LoadUrl(url)
        playlisturl = re.compile('<embed.*file=(.*?)&',flags=re.DOTALL|re.I).findall(html)[0]
        count = 0
        playlisthtml = urltools.LoadUrl(playlisturl)
        line = '|name=Ver Episodio Completo|scraper='+self.scraper+'|type=video|subtype=playall|url='+playlisturl+'|icon='
        print line
        self.AddItem(line)
        if self.EndPage():
            return self.result
        for link in re.compile('<track>.*?<location>(.*?)<\/location>.*?<\/track>',flags=re.DOTALL|re.I).findall(playlisthtml):
            count = count + 1
            line = '|name=Parte '+str(count)+'|scraper='+self.scraper+'|type=video|subtype=video|url='+link+'|icon='
            print line
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result

        
    def Scrape(self,url,startndx):
        self.scraper = 'looktvsscraper'
        self.StartPage(startndx)
        
        what2scrape = 'video'
        
        try:
            what2scrape = re.compile('subtype=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        except:
            pass

        print 'what2scrape: '+what2scrape

        if what2scrape == 'days':
            return self.ScrapeDays(url)
        elif what2scrape == 'novelas':
            return self.ScrapeNovelas(url)
        elif what2scrape == 'video':
            return self.ScrapeVideo(url)

        return 

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        StreamUrl = params['url']
        subtype = params['subtype']
        print 'StreamUrl: '+StreamUrl
        print 'subtype: '+subtype

        if subtype == 'playall':
            playlisthtml = urltools.LoadUrl(StreamUrl)
            UrlList = re.compile('<track>.*?<location>(.*?)<\/location>.*?<\/track>',flags=re.DOTALL|re.I).findall(playlisthtml)
            player.PlayVideoList(UrlList)
        else:
            player.PlayStream(StreamUrl)
        return
    

#------------------------------------------------------------------------------------------------
url1 = 'http://www.look-tvs.com/novelas1.php'
url2 = '|name=21/04 qui|scraper=looktvsscraper|type=dir|subtype=novelas|pageurl=http://www.look-tvs.com/novelas1.php|icon=|day=21/04 qui'
url3 = '|name=Jornal Nacional (Globo)|scraper=looktvsscraper|type=video|subtype=video|pageurl=http://www.look-tvs.com/novelas/jornal/2.html|icon=http://www.look-tvs.com/novela1.jpg'
url4 = 'http://tugatesting.googlecode.com/svn/trunk/channels/novelas.txt?subtype=days&pageurl=http://www.look-tvs.com/novelas1.php& startndx: 0'
url5 = '=http://tugatesting.googlecode.com/svn/trunk/channels/novelas.txt?subtype=novelas&day=21/04 qui&pageurl=http://www.look-tvs.com/novelas1.php&'
#LookTvsScraper().Test(s)
#LookTvsScraper().Scrape(url4,0)
#------------------------------------------------------------------------------------------------
class TvThudinhaScraper(ScraperObj):

    def YouTube(self,url):
        html = urltools.LoadUrl(url)
        ytPlayer = re.compile('<div id="watch-player"(.*?)<noembed>',flags=re.DOTALL|re.I).findall(html)[0]
        print urllib.unquote(ytPlayer)
        return
    def Scrape(self,url,startndx):
        self.scraper = 'looktvscraper'
        self.StartPage(startndx)
        self.url = url
        html = urltools.LoadUrl(url)
        iframeUrl = re.compile('<iframe.*src="(.*?)".*<\/iframe>',flags=re.DOTALL|re.I).findall(html)[0]
        iframeHtml = urltools.LoadUrl(iframeUrl)
        unicodeString = re.compile("document.write\('(.*?)'\)",flags=re.DOTALL|re.I).findall(iframeHtml)[0]
        unicodeString = unicodeString.decode('unicode-escape')
        iframeUrl = re.compile('<iframe.*src="(.*?)".*<\/iframe>',flags=re.DOTALL|re.I).findall(unicodeString)[0]
        iframeHtml = urltools.LoadUrl(iframeUrl)

        fid = re.compile('fid="(.*?)"',flags=re.DOTALL|re.I).findall(iframeHtml)[0]
        yycastUrl = 'http://www.yycast.com/'+fid
        print yycastUrl

        yycastHtml = urltools.LoadUrl(yycastUrl)

        print yycastHtml
        
        return
        #.

        iframeUrl = re.compile('<iframe.*src="(.*?)".*<\/iframe>',flags=re.DOTALL|re.I).findall(html)[0]
        iframeHtml = urltools.LoadUrl(iframeUrl)

        print iframeHtml

        match = urltools.GetAllValues(html,'item')
        self.total = len(match)

        for item in match:
            print item
            name = re.compile('<title><\!\[CDATA\[(.*?)\]\]><\/title>',flags=re.DOTALL|re.I).findall(item)[0]
            pageurl = re.compile('<link><\!\[CDATA\[(.*?)\]\]><\/link>',flags=re.DOTALL|re.I).findall(item)[0]
            videourl = re.compile('<media:content url="(.*?)".*\/>',flags=re.DOTALL|re.I).findall(item)[0]
            icon = re.compile('<media:thumbnail url="(.*?)".*\/>',flags=re.DOTALL|re.I).findall(item)[0]
            line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+videourl+'|icon='+icon
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        StreamUrl = params['url']
        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://www.youtube.com/watch?v=pr4rNzu-DBc'
#TvThudinhaScraper().YouTube(url)
#print urllib.unquote("c.youtube.com%2Fvideoplayback%3Fsparams%3Did%252Cexpire%252Cip%252Cipbits%252Citag%252Calgorithm%252Cburst%252Cfactor%252Coc%253AU0hPRlZPVl9FSkNOOV9OSVpF%26")
#StreamUrl = 'http://v9.lscache6.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hPRlZQUF9FSkNOOV9OSlRB&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&signature=54DAF4EF59666AA041C6EE52F201701AD2A507BF.D09087421EB972ACD0B42E103FB1AC7F76F193A2&expire=1301652000&key=yt1&ip=0.0.0.0&factor=1.25&id=a6be2b373bbe0c17'
#print StreamUrl.decode('unicode-escape')
StreamUrl = '35%7Chttp%3A%2F%2Fv2.lscache3.c.youtube.com%2Fvideoplayback%3Fsparams%3Did%252Cexpire%252Cip%252Cipbits%252Citag%252Calgorithm%252Cburst%252Cfactor%252Coc%253AU0hPRlZQUF9FSkNOOV9OSlRB%26algorithm%3Dthrottle-factor%26itag%3D35%26ipbits%3D0%26burst%3D40%26sver%3D3%26signature%3DD3104424BDACCFA0F07163F51DE36CB5BA5FB105.38B0E983B51E6A9877396AF9A7AAA252B30554D5%26expire%3D1301652000%26key%3Dyt1%26ip%3D0.0.0.0%26factor%3D1.25%26id%3D2299bde0c6698c99%2C34%7Chttp%3A%2F%2Fv2.lscache7.c.youtube.com%2Fvideoplayback%3Fsparams%3Did%252Cexpire%252Cip%252Cipbits%252Citag%252Calgorithm%252Cburst%252Cfactor%252Coc%253AU0hPRlZQUF9FSkNOOV9OSlRB%26algorithm%3Dthrottle-factor%26itag%3D34%26ipbits%3D0%26burst%3D40%26sver%3D3%26signature%3D2D95C76CD06FBB92FAC5F270A270DF5DC9EE62F9.3486582B10E928EA5A7D8848BDF2BA01045DC7D5%26expire%3D1301652000%26key%3Dyt1%26ip%3D0.0.0.0%26factor%3D1.25%26id%3D2299bde0c6698c99%2C18%7Chttp%3A%2F%2Fv22.lscache1.c.youtube.com%2Fvideoplayback%3Fsparams%3Did%252Cexpire%252Cip%252Cipbits%252Citag%252Calgorithm%252Cburst%252Cfactor%252Coc%253AU0hPRlZQUF9FSkNOOV9OSlRB%26algorithm%3Dthrottle-factor%26itag%3D18%26ipbits%3D0%26burst%3D40%26sver%3D3%26signature%3D6C16F90E61702E390E27776918917A02348E9781.9B9FBB5751E5111C6229A1873BC3C0006A8747AD%26expire%3D1301652000%26key%3Dyt1%26ip%3D0.0.0.0%26factor%3D1.25%26id%3D2299bde0c6698c99%2C5%7Chttp%3A%2F%2Fv17.lscache4.c.youtube.com%2Fvideoplayback%3Fsparams%3Did%252Cexpire%252Cip%252Cipbits%252Citag%252Calgorithm%252Cburst%252Cfactor%252Coc%253AU0hPRlZQUF9FSkNOOV9OSlRB%26algorithm%3Dthrottle-factor%26itag%3D5%26ipbits%3D0%26burst%3D40%26sver%3D3%26signature%3D88D1DF6CEDA689B18615D573F5EBA99815109C9F.1AAB37C7E5B032135C23D0F470EF4BC9845D6927%26expire%3D1301652000%26key%3Dyt1%26ip%3D0.0.0.0%26factor%3D1.25%26id%3D2299bde0c6698c99&amp;'
#print urllib.unquote(StreamUrl)
#TvThudinhaScraper().Scrape(url,0)
#------------------------------------------------------------------------------------------------
class TviScraper(ScraperObj):

    def GetVideoList(self,url,startndx):
        print url
        try:
            url = re.compile('(.*?pagina)=\d',flags=re.I).findall(url)[0]
        except:
            pass
        self.url = url+'='+str(startndx)
        print 'self.url: '+self.url
        
        html = urltools.LoadUrl(self.url)
        medialist = re.compile('<div class="accordionVideos"(.*?)<div class="blockPesquisaNavigation"',flags=re.DOTALL|re.I).findall(html)[0]
        match = re.compile('<span>(.*?)<\/span>.*?<a href=".*?mul_id=(.*?)">.*?<div class="blockImageHolder">.*?<img src="(.*?)"',flags=re.DOTALL|re.I).findall(medialist)

        return match
                
    def Scrape(self,url,startndx):
        if startndx==0:
            startndx=1
        self.scraper = 'tviscraper'
        self.pagesize=6
        self.StartPage(startndx)
        self.baseurl = 'http://www.tvi.iol.pt'

        match = self.GetVideoList(url,startndx)

        for item in match:
            #print item
            name = item[0]
            pageurl = self.baseurl+'/videos/video/'+item[1]+'/1'
            icon = item[2]

            self.AddItem('|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon='+icon)

            if self.EndPage():
                return self.result

            '''if self.counter==6:
                self.startndx = self.startndx +1
                match = self.GetVideoList(url,self.startndx)
                for item in match:
                    #print item
                    name = item[0]
                    pageurl = self.baseurl+'/videos/video/'+item[1]+'/1'
                    icon = item[2]
                    self.AddItem('|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon='+icon)
                
                    if self.EndPage():
                        return self.result'''

        return self.result

    def AddItem(self,line):
        print 'counter: '+str(self.counter)
        self.result.append(urltools.GetParams(line))

    def EndPage(self):
        self.counter = self.counter + 1
        if self.counter >= self.pagesize:
            line = '|name=>> Next Page >> '+str(self.startndx+1)+'|scraper='+self.scraper+'|type=dir|url='+self.url+'|startndx='+str(self.startndx+1)
            print line
            self.result.append(urltools.GetParams(line))
            return True
        else:
            return False

    def PlayVideo(self,paramsurl):
        #http://www.tvi24.iol.pt/videos/video/13403248/1
        #mp4:13403248-L-500k

        params=urltools.GetParams(paramsurl)
        url = params['url']
        html = urltools.LoadUrl(url)
        #match = re.compile('<embed.*src="(.*?)".*rtmp.*file=(.*?).mp4',flags=re.DOTALL|re.I).findall(html)
        match = re.compile('\/video\/(.*?)\/',flags=re.DOTALL|re.I).findall(url)

        RtmpUrl   = 'rtmp://video1.iol.pt/vod/'
        PlayPath  = 'mp4:'+match[0]+'-L-500k'
        SwfPlayer = ''
        PageUrl   = url

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        
        return
    
    def ScrapeAllPrograms(self,url):
        html = urltools.LoadUrl(url)
        plist = re.compile('<form id="formProgramas"(.*?)<\/form>',flags=re.DOTALL|re.I).findall(html)[0]

        for value, name in re.compile('<option value="(.*?)">(.*?)<\/option>',flags=re.DOTALL|re.I).findall(plist):
            print '|name='+name+'|scraper=tviscraper|type=dir|url=http://www.tvi.iol.pt/frame_videos_tvi.html?gal_id='+value+'&pagina=1'

        #print plist
        

#------------------------------------------------------------------------------------------------
url = 'http://www.tvi.iol.pt/frame_videos_tvi.html?gal_id=128760'
url = 'http://www.tvi.iol.pt/frame_videos_tvi.html?gal_id=128760&pagina=1'
#print TviScraper().Scrape(url,1)
#------------------------------------------------------------------------------------------------
class RtpVodScraper(ScraperObj):

    def Scrape(self,url,startndx):
        self.scraper = 'rtpvodscraper'
        self.StartPage(startndx)
        self.url = url
        self.baseurl = 'http://www.rtp.pt'
        #print maisrecentes

        action = 'showvideolinks'
        try:
            action = re.compile('subtype=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        except:
            pass
        print url
        print action

        if action == 'showvideolinks':
            html = urltools.LoadUrl(url)
            #print html
            articles = re.compile('<article>(.*?)<\/article>',flags=re.DOTALL|re.I).findall(html)
            for article in articles:
                for item in re.compile('<h2><a href="(.*?)">(.*?)<.*?<img src="(.*?)"',flags=re.DOTALL|re.I).findall(article):
                    name = item[1]
                    videourl = self.baseurl+item[0]
                    icon = item[2]
                    line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+videourl+'|icon='+icon
                    #line = '|name='+name+'|scraper='+self.scraper+'|type=dir|url=http://tugatesting.googlecode.com/svn/trunk/channels/novelas.txt?pageurl='+videourl+'&subtype=showservers&|icon='+icon
                    print line
                    self.AddItem(line)
                    if self.EndPage():
                        return self.result
        elif action == 'showservers':
                pageurl = re.compile('pageurl=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
                print pageurl
                line = '|name=Play Using Server 1|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon=|RtmpUrl=rtmp://195.245.168.31/fastplay'
                self.AddItem(line)
                line = '|name=Play Using Server 2|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon=|RtmpUrl=rtmp://62.41.78.101/fastplay'
                self.AddItem(line)
                line = '|name=Play Using Server 3|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon=|RtmpUrl=rtmp://62.41.78.102/fastplay'
                self.AddItem(line)
                line = '|name=Play Using Server 4|scraper='+self.scraper+'|type=video|url='+pageurl+'|icon=|RtmpUrl=rtmp://195.245.168.33/fastplay'
                self.AddItem(line)
 
        return self.result

    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        try:
            url = params['url']
        except:
            url = paramsurl
        mainpage = urltools.LoadUrl(url)
        print 'STARTURL:' + url

        partes = re.compile('<ul class="TabsDestk Partes">(.*?)<\/ul>',flags=re.DOTALL|re.I).findall(mainpage)[0]
        print "<PARTES>"
        print partes
        print "</PARTES>"
        RtmpList = []
        count = 0
        for parte in re.compile('<a href="(.*?)"',flags=re.DOTALL|re.I).findall(partes):
            count = count + 1
            if count > 2:
                break
            PageUrl = "http://www.rtp.pt"+parte

            html = urltools.LoadUrl(PageUrl)
            match = re.compile('<script.*(new SWFObject.*?)<\/script>',flags=re.DOTALL|re.I).findall(html)[0]
            #print match
            RtmpUrl   = re.compile('"streamer",\s{0,1}"(.*?)"',flags=re.DOTALL|re.I).findall(match)[0]
            PlayPath  = "mp4:"+re.compile('"file",\s{0,1}"(.*?)"',flags=re.DOTALL|re.I).findall(match)[0]
            SwfPlayer = re.compile('new SWFObject\("(.*?)"',flags=re.DOTALL|re.I).findall(match)[0]

            #print 'RtmpUrl: '+RtmpUrl
            #print 'PlayPath: '+PlayPath
            #print 'SwfPlayer: '+SwfPlayer
            #print 'PageUrl: '+PageUrl
            RtmpList.append([PageUrl, SwfPlayer, PlayPath, RtmpUrl])
            

        print RtmpList
        player.PlayRtmpList(RtmpList)
            
        return
        
        #print html
        match = re.compile('<script.*(new SWFObject.*?)<\/script>',flags=re.DOTALL|re.I).findall(html)[0]
        print match

        RtmpUrl   = re.compile('"streamer",\s{0,1}"(.*?)"',flags=re.DOTALL|re.I).findall(match)[0]
        PlayPath  = "mp4:"+re.compile('"file",\s{0,1}"(.*?)"',flags=re.DOTALL|re.I).findall(match)[0]
        SwfPlayer = re.compile('new SWFObject\("(.*?)"',flags=re.DOTALL|re.I).findall(match)[0]
        PageUrl   = url

        #print 'RtmpUrl: '+RtmpUrl
        #print 'PlayPath: '+PlayPath
        #print 'SwfPlayer: '+SwfPlayer
        #print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        return

    def GetAllList(self,url):
        #url = 'http://www.rtp.pt/multimediahtml/php/ajax_links.php?menu=video&pesquisa=canal'
        baseurl = "http://www.rtp.pt/multimediahtml/"
        html = urltools.LoadUrl(url)
        match = re.compile("<li><a href='(.*?)'>(.*?)<\/a><\/li>",flags=re.DOTALL|re.I).findall(html)
        for progurl,name in match:
            line = '|name='+name+'|scraper=rtpvodscraper|type=dir|url='+baseurl+progurl+'|icon='
            print line
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for name in re.compile("name=(.*?)\|",flags=re.DOTALL|re.I).findall(html):
        #for name in re.compile("prog=(.*?)\|",flags=re.DOTALL|re.I).findall(html):
            print name
        
            
    

#------------------------------------------------------------------------------------------------
url = "http://www.rtp.pt/programa/episodios/tv/p1103"
#RtpVodScraper().Scrape(url,1)
#url = "http://tugatesting.googlecode.com/svn/trunk/channels/rtprtpmadeira.txt"
url = "http://www.rtp.pt/programa/tv/p1103/c103113"
url = "http://www.rtp.pt/programa/tv/p1103/c106812"
url = "http://www.rtp.pt/programa/tv/p1103/c106717"
#RtpVodScraper().PlayVideo(url)
#------------------------------------------------------------------------------------------------
class TvFutebolScraper(ScraperObj):

    def Scrape(self,url,startndx):
        self.scraper = 'rtpvodscraper'
        self.StartPage(startndx)
        self.url = url
        self.baseurl = 'http://www.rtp.pt/multimediahtml/'
        html = urltools.LoadUrl(url)
        maisrecentes = re.compile('<div class="navRecentes">(.*?)<!--navRecentes-->',flags=re.DOTALL|re.I).findall(html)[0]
        #print maisrecentes

        for item in re.compile('<a href="(.*?)"><img src="(.*?)".*?title="(.*?)" \/>.*?<div class="dataVid">(.*?)<\/div><\/a><\/div>',flags=re.DOTALL|re.I).findall(maisrecentes):
            name = item[3]+' - '+item[2]
            videourl = self.baseurl+item[0]
            icon = item[1]
            line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+videourl+'|icon='+icon
            print line
            self.AddItem(line)
            if self.EndPage():
                return self.result

        return self.result

    def GetChannelName(self,url):
        print url
        try:
            channelName = re.compile('chname=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        except:
            channelName = re.compile('c=(.*?)&',flags=re.DOTALL|re.I).findall(url)[0]
        #channelName = re.compile('in\/(.*?)$',flags=re.DOTALL|re.I).findall(url)[0]
        return channelName
            
        
        
    def GetRefererUrl(self,url):
        channelName = self.GetChannelName(url)
        url = "http://www.estadio.in/embed.php?c=CHANNEL_NAME&height=390&width=480&stretching=exactfit&logofile=&logolink=http://www.look-tvs.com&logoposition=random&image=&skin=&autostart=true"
        url = "http://www.estadio.in/CHANNEL_NAME"
        return url.replace("CHANNEL_NAME",channelName);

    def GetIframeUrl(self,url):
        channelName = self.GetChannelName(url)
        url = "http://www.televisaofutebol.com/iframe.php?chname=CHANNEL_NAME&height=390&width=480&stretching=exactfit&logofile=&logolink=http://www.look-tvs.com&logoposition=random&image=&skin=&abouttext=http://www.look-tvs.com&autostart=true"
        return url.replace("CHANNEL_NAME",channelName);


    def PlayVideo(self,paramsurl):
        params=urltools.GetParams(paramsurl)
        try:
            url = params['url']
        except:
            url = paramsurl
        refUrl = self.GetRefererUrl(url)
        print 'refUrl: '+refUrl
        html = urltools.LoadUrlRef(url,refUrl)
        print html
        
        match = re.compile('<embed.*file=(.*?)&.*streamer=(.*?)&.*src="(p.*?.swf)"',flags=re.DOTALL|re.I).findall(html)
        #print match

        RtmpUrl   = match[0][1]
        PlayPath  = match[0][0].replace(".flv","")
        SwfPlayer = 'http://www.televisaofutebol.com/'+match[0][2]
        PageUrl   = url #self.GetIframeUrl (url)

        print 'RtmpUrl: '+RtmpUrl
        print 'PlayPath: '+PlayPath
        print 'SwfPlayer: '+SwfPlayer
        print 'PageUrl: '+PageUrl

        player.PlayRtmpStream(PageUrl, SwfPlayer, PlayPath, RtmpUrl)
        return

    def GetAllList(self,url):
        #url = 'http://www.rtp.pt/multimediahtml/php/ajax_links.php?menu=video&pesquisa=canal'
        baseurl = "http://www.rtp.pt/multimediahtml/"
        html = urltools.LoadUrl(url)
        match = re.compile("<li><a href='(.*?)'>(.*?)<\/a><\/li>",flags=re.DOTALL|re.I).findall(html)
        for progurl,name in match:
            line = '|name='+name+'|scraper=rtpvodscraper|type=dir|url='+baseurl+progurl+'|icon='
            print line
            
    

#------------------------------------------------------------------------------------------------
#url = "http://www.estadio.in/embed.php?c=tvi24&height=390&width=480&stretching=exactfit&logofile=&logolink=http://www.look-tvs.com&logoposition=random&image=&skin=&autostart=true;"
url = "http://www.televisaofutebol.com/iframe.php?chname=tvi24&height=400&width=600&stretching=exactfit&logofile=http://www.televisaofutebol.com/images/logostream.png&logolink=http://www.estadio.in/tvi24&logoposition=top-left&image=http://www.televisaofutebol.com/images/fundostream.png&skin=&abouttext=http://www.estadio.in/tvi24&autostart=true"
sicn = "http://www.estadio.in/embed_televisaofutebol.php?c=sicnoticias&height=500&width=650&stretching=exactfit&logofile=http://www.televisaofutebol.com/images/logostream.png&logolink=http://www.estadio.in/sicnoticias&logoposition=random&image=http://www.televisaofutebol.com/images/fundostream.png&skin=&autostart=true"

#url = "http://www.estadio.in/sicnoticias"
#TvFutebolScraper().PlayVideo(axn)
#------------------------------------------------------------------------------------------------
class SicNoticiasScraper(ScraperObj):
    def Test(self,url):
        html = urltools.LoadUrl(url)
        for item in urltools.GetAllValues(html,'channel'):
            name = urltools.GetValue(item,'pageurl')
            print name

    def Scrape(self,url,startndx):
        self.scraper = 'sicnoticiasscraper'
        self.StartPage(startndx)
        self.url = url
        baseurl = 'http://sicnoticias.sapo.pt'
        url = url.replace(' ','+')
        print 'Scraping: '+str(url)
        html = urltools.LoadUrl(url)

        #all other videos
        rssurl = re.compile('<link rel="alternate" type="application\/rss\+xml" title=".*" href="(.*widget=rssfeed.*?)" \/>',flags=re.DOTALL|re.I).findall(html)[0]
        rssurl = rssurl.replace('amp;','')
        rsshtml = urltools.LoadUrl(rssurl)
        print rssurl

        match = re.compile('<item(.*?)<\/item>',flags=re.DOTALL|re.I).findall(rsshtml)
        for item in match:
            name = re.compile('<title>(.*?)<\/title>',flags=re.DOTALL|re.I).findall(item)[0]
            pageurl = re.compile('<link><\!\[CDATA\[(.*?)\]\]><\/link>',flags=re.DOTALL|re.I).findall(item)[0]
            pubDate = re.compile('<pubDate>(.*?)<\/pubDate>',flags=re.DOTALL|re.I).findall(item)[0]
            description = re.compile('<description>(.*?)<\/description>',flags=re.DOTALL|re.I).findall(item)[0]
            name = pubDate + ' - ' + name + ' - ' + description
            icon =  ''
            line = '|name='+name+'|scraper='+self.scraper+'|type=video|url='+pageurl+"|icon="+icon
            print line
            self.AddItem(line)
            if self.EndPage():
               return self.result

        return self.result


    def PlayVideo(self,paramsurl):
        baseurl = 'http://sicnoticias.sapo.pt'
        params=urltools.GetParams(paramsurl)
        try:
           VideoPageUrl = params['url']
        except:
           VideoPageUrl = paramsurl
        html = urltools.LoadUrl(VideoPageUrl)
        StreamUrl = re.compile("{.file:'(.*)',.image",flags=re.DOTALL|re.I).findall(html)[0]

        print 'StreamUrl: '+StreamUrl

        player.PlayStream(StreamUrl)
        
        return
    

#------------------------------------------------------------------------------------------------
url = 'http://sicnoticias.sapo.pt/programas/jornaldanoite/'
#SicNoticiasScraper().Scrape(url,0)
url = 'http://sicnoticias.sapo.pt/programas/jornaldanoite/2012/08/29/edicao-de-29-08-2012-1-parte-1'
#SicNoticiasScraper().PlayVideo(url)
#------------------------------------------------------------------------------------------------
